<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 消息 model
 *
 */
class Messagesmodel extends CI_Model {
	function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 取得具体会员的消息列表
     *
     * @param unknown_type $uid
     * @return unknown
     */
    public function get_messages($uid='')
    {
    	$result = array();
    	$this->dblib->set_query_limit();
    	$this->db->select('m.id, m.uid, m.fanid, u.name, u.photo, m.msg, m.createtime')
				->from('user as u')
				->join('message as m', 'm.fanid=u.id')
				->where('m.uid', $uid)
				->order_by('m.createtime', 'desc');
		$query = $this->db->get();
		if($query->num_rows() > 0){
			//如果有结果集
			$this->load->library('uploadimglib');
			$result = $query->result_array();
			//设置photo路径
			foreach ($result as $k=>$v){
				$result[$k]['photo'] = $this->uploadimglib->re_image_path($v['photo']);
			}
		}
		return $result;
    }
    
    /**
     * 取得具体某一条消息
     *
     * @param unknown_type $id
     * @return unknown
     */
    public function get_message($id='', $uid='')
    {
    	$result = array();
    	$this->db->select('m.id, m.uid, m.fanid, u.name, u.photo, m.msg, m.createtime')
    			->from('user as u')
				->join('message as m', 'm.fanid=u.id')
				->where(array('m.id' => $id, 'm.uid' => $uid));
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			$this->load->library('uploadimglib');
			$result = $query->result_array();
			//设置photo路径
			foreach ($result as $k=>$v){
				$result[$k]['photo'] = $this->uploadimglib->re_image_path($v['photo']);
			}
		}
		return $result;
    }
    
    /**
     * 添加消息（发送）
     *
     * @param unknown_type $data
     * @return unknown
     */
    public function insert_message($data)
    {
    	if (!isset($data['uid']) || empty($data['uid']) 
    			|| !isset($data['fanid']) || empty($data['fanid'])
    			|| !isset($data['msg']) || empty($data['msg'])) {
    		return false;
    	}
    	
    	$this->db->set('id', '(SELECT UUID())', FALSE);
    	return $this->db->insert('message', $data);
    }
}

/* End of file messagesmodel.php */
/* Location: ./application/models/messagesmodel.php */